package com.vibeplayer.app.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.vibeplayer.app.R;
import com.vibeplayer.app.adapter.PlaylistAdapter;
import com.vibeplayer.app.db.DatabaseHelper;
import com.vibeplayer.app.model.Playlist;

import java.util.ArrayList;
import java.util.List;

public class PlaylistsFragment extends Fragment implements PlaylistAdapter.PlaylistClickListener {

    private RecyclerView recyclerView;
    private PlaylistAdapter adapter;
    private List<Playlist> playlists = new ArrayList<>();
    private TextView txtNoPlaylists;
    private FloatingActionButton fabAddPlaylist;
    private DatabaseHelper databaseHelper;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_playlists, container, false);
        
        recyclerView = view.findViewById(R.id.recyclerViewPlaylists);
        txtNoPlaylists = view.findViewById(R.id.txtNoPlaylists);
        fabAddPlaylist = view.findViewById(R.id.fabAddPlaylist);
        
        // 初始化数据库帮助类
        databaseHelper = DatabaseHelper.getInstance(requireContext());
        
        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new PlaylistAdapter(getContext(), playlists, this);
        recyclerView.setAdapter(adapter);
        
        // 设置添加播放列表按钮点击事件
        fabAddPlaylist.setOnClickListener(v -> showCreatePlaylistDialog());
        
        // 加载播放列表
        loadPlaylists();
        
        return view;
    }
    
    @Override
    public void onResume() {
        super.onResume();
        // 刷新播放列表
        loadPlaylists();
    }
    
    private void loadPlaylists() {
        playlists.clear();
        playlists.addAll(databaseHelper.getAllPlaylists());
        
        if (playlists.isEmpty()) {
            txtNoPlaylists.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            txtNoPlaylists.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged();
        }
    }
    
    private void showCreatePlaylistDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        builder.setTitle(R.string.create_playlist);
        
        // 设置输入框
        final EditText input = new EditText(requireContext());
        input.setHint(R.string.playlist_name);
        builder.setView(input);
        
        // 设置按钮
        builder.setPositiveButton(R.string.save, (dialog, which) -> {
            String playlistName = input.getText().toString().trim();
            if (!playlistName.isEmpty()) {
                // 创建新播放列表
                long id = databaseHelper.createPlaylist(playlistName);
                if (id != -1) {
                    // 刷新播放列表
                    loadPlaylists();
                }
            }
        });
        builder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.cancel());
        
        builder.show();
    }
    
    @Override
    public void onPlaylistClick(int position) {
        // 打开播放列表详情
        Playlist playlist = playlists.get(position);
        PlaylistDetailFragment fragment = PlaylistDetailFragment.newInstance(playlist.getId());
        
        // 隐藏ViewPager，显示Fragment容器
        View viewPager = requireActivity().findViewById(R.id.viewPager);
        View fragmentContainer = requireActivity().findViewById(R.id.fragmentContainer);
        viewPager.setVisibility(View.GONE);
        fragmentContainer.setVisibility(View.VISIBLE);
        
        // 替换Fragment
        requireActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragmentContainer, fragment)
                .addToBackStack(null)
                .commit();
    }
} 